package com.huacai.merchant.service.impl;

import java.util.List;

import com.huacai.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huacai.merchant.mapper.MerchantMapper;
import com.huacai.merchant.domain.Merchant;
import com.huacai.merchant.service.IMerchantService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import static com.huacai.common.utils.SecurityUtils.getUserId;

/**
 * 商家Service业务层处理
 *
 * @author hsj
 * @date 2025-05-12
 */
@Service
public class MerchantServiceImpl implements IMerchantService
{
    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询商家
     *
     * @param merchantId 商家主键
     * @return 商家
     */
    @Override
    public Merchant selectMerchantByMerchantId(String merchantId)
    {
        return merchantMapper.selectMerchantByMerchantId(merchantId);
    }

    /**
     * 查询商家列表
     *
     * @param merchant 商家
     * @return 商家
     */
    @Override
    public List<Merchant> selectMerchantList(Merchant merchant)
    {
        return merchantMapper.selectMerchantList(merchant);
    }

    /**
     * 新增商家
     *
     * @param merchant 商家
     * @return 结果
     */
    @Override
    public int insertMerchant(Merchant merchant)
    {
        merchant.setMerchantId(IdUtils.fastSimpleUUID());
        merchant.setUserId(Math.toIntExact(getUserId()));
        return merchantMapper.insertMerchant(merchant);
    }

    /**
     * 批量新增商家
     *
     * @param merchants 商家List
     * @return 结果
     */
    @Override
    public int batchInsertMerchant(List<Merchant> merchants)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(merchants)) {
            try {
                for (int i = 0; i < merchants.size(); i++) {
                    int row = merchantMapper.insertMerchant(merchants.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == merchants.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改商家
     *
     * @param merchant 商家
     * @return 结果
     */
    @Override
    public int updateMerchant(Merchant merchant)
    {
        return merchantMapper.updateMerchant(merchant);
    }

    /**
     * 批量删除商家
     *
     * @param merchantIds 需要删除的商家主键
     * @return 结果
     */
    @Override
    public int deleteMerchantByMerchantIds(String[] merchantIds)
    {
        return merchantMapper.deleteMerchantByMerchantIds(merchantIds);
    }

    /**
     * 删除商家信息
     *
     * @param merchantId 商家主键
     * @return 结果
     */
    @Override
    public int deleteMerchantByMerchantId(String merchantId)
    {
        return merchantMapper.deleteMerchantByMerchantId(merchantId);
    }
    /**
     * 查询商家表当中是否存在某个userId
     * @param userId 用户Id
     * @return 存在返回true
     */
    @Override
    public Integer selectMerchantWhereUserId(Integer userId) {
        return merchantMapper.selectMerchantWhereUserId(userId);
    }

    /**
     * 根据userId查询商家信息
     */
    @Override
    public Merchant selectMerchantByUserId(Integer userId) {
        return merchantMapper.selectMerchantByUserId(userId);
    }
}
